﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Management.Automation;
using PowerOBIProvider;
using System.Collections.ObjectModel;
using PowerOBIProvider.OBI;
using PowerOBIPRovider.OBIWebReference;
namespace PowerOBIProvider.Cmdlts
{
    [Cmdlet(VerbsCommon.New, "Account",
    SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
    public class NewAccountCmdlt : BaseOBICmdlt
    {
        private string accountName = null;
        [Parameter(Mandatory = true, Position = 1)]
        [ValidateNotNullOrEmpty]
        public string AccountName
        {
            get
            {
                return accountName;
            }
            set
            {
                accountName = value;
            }
        }
        
        private string drive = null;
        [Parameter(Mandatory = false, Position = 2)]
        [ValidateNotNullOrEmpty]
        public string Drive
        {
            get
            {
                return drive;
            }
            set
            {
                drive = value;
            }
        }
        
        
        
        private bool groupAccount = true;
        [Parameter(Mandatory = false, Position = 3)]
        [ValidateNotNullOrEmpty]
        public SwitchParameter GroupAccount
        {
            get
            {
                return groupAccount;
            }
            set
            {
                groupAccount = value;
            }

        }
        private bool userAccount = false;
        [Parameter(Mandatory = false, Position = 4)]
        [ValidateNotNullOrEmpty]
        public SwitchParameter UserAccount
        {
            get
            {
                return userAccount;
            }
            set
            {
                userAccount = value;
            }
        }
        

        protected override void ProcessRecord()
        {
            PSOBIDriveInfo obiDrive = null;
            if (drive == null)
            {
                obiDrive = GetOBIDrive(SessionState.Path.CurrentLocation);
            }
            else
            {
                obiDrive = SessionState.Drive.Get(drive) as PSOBIDriveInfo;
            }
            if ( this.ShouldProcess(accountName,
                "create a new WebCatalog account") )
            {
                try
                {
                    Account newAccount = new Account();
                    newAccount.name = accountName.Trim();
                    if ( groupAccount )
                        newAccount.accountType = 1;
                    else if ( userAccount )
                        newAccount.accountType = 0;
                    obiDrive.ObiWSClient.CreateAccount(newAccount);
                }
                catch ( Exception ex )
                {
                    WriteError(new ErrorRecord(ex, "0", ErrorCategory.OperationStopped, null));
                }
            }
        }
    }
    
}
